<?php
class hschool_course_hour{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*	取出一个课程信息
	*/
	public function get_one($id='') {
		$info = array();
		if($id){
			$sql = "SELECT * FROM ".DB::table('course_hour')." WHERE id='".$id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}

	/*
	*	创建课程
	*/
	public function add() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		$course_id = isset($_REQUEST['course_id']) ? $_REQUEST['course_id']:'';
		if(!empty($is_submit)){
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			$data = array(
				'name' => $_REQUEST['name'],
				'title_page' => $_REQUEST['title_page'],
				'files' => implode(',',$_REQUEST['files']),
				'student_files' => implode(',',$_REQUEST['student_files']),
				'create_id' => $_G['user_id'],
				'teacher_id' => trim($_REQUEST['teacher_id']),
				'assistants' => trim($_REQUEST['assistants']),
				'introduction' => $_REQUEST['introduction'],
				'start_time' => strtotime($_REQUEST['start_time']),
				'end_time' => strtotime($_REQUEST['end_time']),
				'vod_url' => $_REQUEST['vod_url'],
				'duration' => ceil(((strtotime($_REQUEST['end_time'])-strtotime($_REQUEST['start_time']))/60)),
				'dateline' => TIMESTAMP,
				'subject_id' => $_REQUEST['subject_id'],
				'related_course_hour' => $_REQUEST['related_course_hour']
			);
			if($id){
				$effect = DB::update('course_hour',$data,array('id'=>$id));
			}else{
				$data['course_id'] = $course_id;
				$effect = DB::insert('course_hour',$data,1);
			}
			if($effect){
				$return_data = array('course_id'=>$course_id);
				self::return_json(false,'操作成功',$return_data);
			}else{
				self::return_json();
			}
		}else{
			$hschool_course = load_class('hschool','course');
			if($hschool_course){
				$subject_array = $hschool_course->get_subject_array();
			}
			include template('admin/hschool/course_hour_add');	
		}
	}

	/*
	*	编辑课程
	*/
	public function edit() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){

		}else{
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			$info = $this->get_one($id);
			$course_id=$info['course_id'];
			//DEBUG 取出文件库存储的资源文件
			$mul = strpos($info['files'],',');
			if($mul){
				$file_ids = explode(',',$info['files']);
			}else{
				$file_ids[] = $info['files'];
			}
			if($file_ids){
				$hschool_files = load_class('hschool','files');
				if($hschool_files && $file_ids){
					$more='';
					$more = $hschool_files->get_more($file_ids);
				}
			}
			//DEBUG 取出文件库存储的资源文件
			$mul_student = strpos($info['student_files'],',');
			if($mul_student){
				$student_file_ids = explode(',',$info['student_files']);
			}else{
				$student_file_ids[] = $info['student_files'];
			}
			if($student_file_ids){
				$hschool_files = load_class('hschool','files');
				if($hschool_files && $student_file_ids){
					$more_student = '';
					$more_student = $hschool_files->get_more($student_file_ids);
				}
			}
			$hschool_course = load_class('hschool','course');
			if($hschool_course){
				$subject_array = $hschool_course->get_subject_array();
			}
			include template('admin/hschool/course_hour_add');
		}
	}

	/*
	*	编辑课程
	*/
	public function stat() {
		global $_G;
		$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
		$is_over = isset($_REQUEST['is_over']) ? $_REQUEST['is_over'] : '';
		if(!empty($id)){
			$info = $this->get_one($id);
			//DEBUG 取出本课时参与上课人员的情况统计
			$page_array = array();
			$sqlcount = $sql = $wheresql = $title_keyword = $pageNum = $numPerPage = '';
			$pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
			$numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 10;
			$pagestart = ($pageNum - 1) * $numPerPage;
			$sqlcount = "SELECT count(*) from ".DB::table('student_course_record')." WHERE course_lesson_id='".$id."' ".$wheresql;
			$pagetotal = DB::result_first($sqlcount);
			$sql = "SELECT id,student_id,start_time,end_time from ".DB::table('student_course_record')." WHERE course_lesson_id='".$id."' ".$wheresql." ORDER BY id DESC LIMIT ".$pagestart.",".$numPerPage;
			$page_array = DB::fetch_all($sql);
			foreach($page_array AS $key => $value){
				$page_array[$key]['user_realname']=ext::user_realname($value['student_id']);
			}
			//总页数
			$realpages = @ceil($pagetotal / $numPerPage);
			if($realpages <= 0){
				$realpages=1;
			}
			//前一页
			$page_pre = max(1, intval(($pageNum-1)));
			//后一页
			$page_next = min(intval(($pageNum+1)), $realpages);
			//DEBUG 获取课程类型数组
			//DEBUG 总计人数
			$course_hour_count=0;
			$sqlcount_user_id = "SELECT count(DISTINCT student_id) from ".DB::table('student_course_record')." WHERE course_lesson_id='".$id."' ";
			$course_hour_count = DB::result_first($sqlcount_user_id);
			if(empty($course_hour_count)){
				$course_hour_count='未获取';
			}
			include template('admin/hschool/course_hour_stat');
		}
	}

	/*
	*	删除课时
	*/
	public function del() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
			if(!empty($is_submit)){
			$course_hour_ids_array = array();
			$course_hour_ids_array = isset($_REQUEST['course_hour_ids']) ? $_REQUEST['course_hour_ids']:array();
			if($course_hour_ids_array){
				$update = array(
					'isdelete'=> 1
				);
				$where = ' id IN ('.dimplode($course_hour_ids_array).') ';
				$effects = DB::update('course_hour',$update,$where);
			}
			//DEBUG 接收用户信息 end
			$this->return_json(false,'操作成功');
			echo json_encode($return_array);
		}
	}

	/*
	*	当前动作的首页
	*/
	public function index() {
		global $_G;
		$page_array = array();
		$sqlcount = $sql = $wheresql = $title_keyword = $pageNum = $numPerPage = '';
		$pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
		$numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 50;
		$pagestart = ($pageNum - 1) * $numPerPage;
		$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
		$course_hour_name = isset($_REQUEST['course_hour_name']) ? $_REQUEST['course_hour_name'] : '';
		$is_over = isset($_REQUEST['is_over']) ? $_REQUEST['is_over'] : '';
		if($course_hour_name){
			$wheresql .= " AND c.name LIKE '%".$course_hour_name."%'";
		}
		if($is_over){
			$wheresql .= " AND c.start_time < ".TIMESTAMP;
		}
		if($id){
			$wheresql .= " AND c.course_id = '".$id."'";
		}
		$sqlcount = "SELECT count(*) from ".DB::table('course_hour')." AS c WHERE c.isdelete=0 ".$wheresql;
		$pagetotal = DB::result_first($sqlcount);
		$sql = "SELECT c.* from ".DB::table('course_hour')." AS c WHERE c.isdelete=0 ".$wheresql." ORDER BY c.start_time DESC LIMIT ".$pagestart.",".$numPerPage;
		$page_array = DB::fetch_all($sql);
		foreach($page_array AS $key => $value){
			//DEBUG 取出授课老师名称
			$page_array[$key]['teacher_name']=ext::user_realname($value['teacher_id']);
			//DEBUG 取出助教老师名称
			$mul = strpos($value['assistants'],',');
			if(!$mul && !empty($value['assistants'])){
				$page_array[$key]['assistants_str']=ext::user_realname($value['assistants']);
			}else{
				$assistants = explode(',',$value['assistants']);
				$assistants_name = ext::user_realname($assistants);
				$page_array[$key]['assistants_str']=implode(',',$assistants_name);
			}
			$page_array[$key]['course_cateid1'] = DB::result_first("SELECT course_cateid1 FROM ".DB::table('course')." WHERE id='".$value['course_id']."' LIMIT 1");
		}
		//总页数
		$realpages = @ceil($pagetotal / $numPerPage);
		if($realpages <= 0){
			$realpages=1;
		}
		//前一页
		$page_pre = max(1, intval(($pageNum-1)));
		//后一页
		$page_next = min(intval(($pageNum+1)), $realpages);
		//DEBUG 获取课程类型数组
		include template('admin/hschool/course_hour');
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error='true',$message="操作失败",$data=array()) {
		$return_array = array(
			"error" => $error,
			"message" => $message,
			"data" => $data
		);
		echo json_ext($return_array);
		die;
	}
    
    /*
     * 获取用户进入课时的文件资源
     * @pram type int course_hour 根据课时取 user 根据用户取
     * @pram id int 课时编号 或者 用户编号
     */
	public function get_course_hour_files($type='',$id='') {
		$files = array();
        $files_ids='';
		if($id){
            if('course_hour'==$type){
                $sql = "SELECT files FROM ".DB::table('course_hour')." WHERE id='".$id."' LIMIT 1";
                $files_ids = DB::result_first($sql);
            }elseif('user'==$type){
                //DEBUG 取出用户的全部选课
                $sql = "SELECT course_id FROM ".DB::table('student_course')." WHERE uid='".$id."'";
                $course_id_array = DB::fetch_all($sql);
                foreach($course_id_array AS $key => $value){
                    $user_course_id[$key] = $value['course_id'];
                }
                //DEBUG 取出今天的全部课时
                $sql = "SELECT files FROM ".DB::table('course_hour')." WHERE course_id IN (".dimplode($user_course_id).") AND FROM_UNIXTIME( start_time, '%Y%m%d' ) = '".date('Ymd',TIMESTAMP)."'";
                $files_id_array = DB::fetch_all($sql);
                foreach($files_id_array AS $key => $value){
                    $user_files_id[$key] = $value['files'];
                }
                if(!empty($user_files_id)){
                   $files_ids = implode(',',$user_files_id); 
                }
            }
            $where_sql = '';
            if(strpos($files_ids,",")){
                $files_id_array = explode(',',$files_ids);
                $where_sql = " AND file_id IN (".dimplode($files_id_array).")";
            }else{
                $where_sql = " AND file_id = '".$files_ids."' LIMIT 1";
            }
			$sql = "SELECT file_name,file_extension,file_store_path,file_real_name FROM ".DB::table('files')." WHERE isdelete=0 ".$where_sql;
			$files_result = DB::fetch_all($sql);
            foreach($files_result AS $key => $value){
                $files[$key]['url']='http://'.$_SERVER["HTTP_HOST"].'/'.SITE_FILE_ROOT.$value['file_store_path'].'/'.$value['file_real_name'].'.'.$value['file_extension'];
                $files[$key]['md5']=md5_file(SITE_ROOT.'./'.SITE_FILE_ROOT.$value['file_store_path'].'/'.$value['file_real_name'].'.'.$value['file_extension']);
            }
		}
        $return = '';
        $return_array = array();
        if($files){
            foreach($files AS $key => $value){
                $return_array[$key]= $value['url'].'||'.$value['md5']; 
            }
        }
		return implode(',',$return_array);
	}
}
?>